              //
//  GHUtils.m
//  GodHorses
//
//  Created by coder on 2017/11/14.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "TMUtils.h"
#import "STTools_VideoPlayViewController.h"
#import "Qiniu-umbrella.h"
#import "THVideoDetailViewController.h"
#import "THTieZiDetailViewController.h"
#import "THShareViewController.h"

#import "THOtherUserInfoViewController.h"
#import "THMyUserInfoViewController.h"
@implementation TMUtils
+ (BOOL)stoneobs_fun_isIphoneX{
    
    if (@available(iOS 11.0, *)) {
        if (UIApplication.sharedApplication.keyWindow.safeAreaInsets.bottom) {
            return YES;
        }
    } else {
        // Fallback on earlier versions
    }
    
    return NO;
}
+ (void)stoneobs_fun_netWorkMonitorinCanUseWithHandle:(void (^)(void))handle{
    
    AFNetworkReachabilityManager *stoneobs_temp_manager = [AFNetworkReachabilityManager sharedManager];
    [stoneobs_temp_manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        if (status == AFNetworkReachabilityStatusReachableViaWWAN || status == AFNetworkReachabilityStatusReachableViaWiFi) {
            if (handle) {
                handle();
            }
        }
    }];
    [stoneobs_temp_manager startMonitoring];
}
+ (UIView*)stoneobs_fun_headerViewWithTitle:(NSString*)title{
    UIView * stoneobs_temp_sectionHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, STONEOBS_UI_UISCREEN_WIDTH, 44)];
    stoneobs_temp_sectionHeader.backgroundColor = [UIColor whiteColor];
    STLabel * stoneobs_temp_titleLable = [[STLabel alloc] initWithFrame:CGRectMake(15, 0, STONEOBS_UI_UISCREEN_WIDTH - 30, 44)
                                                     text:title
                                                textColor:STONEOBS_UI_FIRST_TEXT_COLOR
                                                     font:14
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    
    [stoneobs_temp_sectionHeader addSubview:stoneobs_temp_titleLable];
    return stoneobs_temp_sectionHeader;
}

+ (CGFloat)stoneobs_fun_get_navgationBarBootom{
    
    UIWindow * stoneobs_temp_window = UIApplication.sharedApplication.delegate.window;
    if (@available(iOS 11.0, *)) {
        if (stoneobs_temp_window.safeAreaInsets.bottom) {
            return 88;
        }
    } else {
        // Fallback on earlier versions
    }
    
    return 64;
}
+ (CGFloat)stoneobs_fun_get_tabBarTop{
    
    UIWindow * stoneobs_temp_window = UIApplication.sharedApplication.delegate.window;
    if (@available(iOS 11.0, *)) {
        if (stoneobs_temp_window.safeAreaInsets.bottom) {
            return (STONEOBS_UI_UISCREEN_HEIGHT - 83);
        }
    } else {
        // Fallback on earlier versions
    }
    
    return (STONEOBS_UI_UISCREEN_HEIGHT - 49);
}
+ (UITextField*)stoneobs_fun_textFiledWithLeftTitle:(NSString*)leftTitle placeHolader:(NSString*)placeHolader  rightView:(UIView*)rightView{
    UITextField * stoneobs_temp_textFiled = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, STONEOBS_UI_UISCREEN_WIDTH, 50)];
    STButton * stoneobs_temp_buyButton = [[STButton alloc] initWithFrame:CGRectMake(0, 0, 90, 50)
                                                     title:leftTitle
                                                titleColor:STONEOBS_UI_FIRST_TEXT_COLOR
                                                 titleFont:15
                                              cornerRadius:0
                                           backgroundColor:nil
                                           backgroundImage:nil
                                                     image:nil];
    stoneobs_temp_buyButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    stoneobs_temp_textFiled.leftView = stoneobs_temp_buyButton;
    stoneobs_temp_textFiled.leftViewMode = UITextFieldViewModeAlways;
    stoneobs_temp_buyButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    stoneobs_temp_textFiled.placeholder = placeHolader;
    
    stoneobs_temp_textFiled.rightView = rightView;
    stoneobs_temp_textFiled.rightViewMode = UITextFieldViewModeAlways;
    return stoneobs_temp_textFiled;
}
//黄色按钮 和 更多
+ (STButton*)stoneobs_fun_titleThemeButtonViewWithTitle:(NSString*)title showRightGo:(BOOL)showRightGo handle:(void(^)(void))handle{
    STButton * stoneobs_temp_buyButton = [[STButton alloc] initWithFrame:CGRectMake(0, 0, STONEOBS_UI_UISCREEN_WIDTH, 44)
                                                     title:title
                                                titleColor:STONEOBS_UI_FIRST_TEXT_COLOR
                                                 titleFont:14
                                              cornerRadius:0
                                           backgroundColor:nil
                                           backgroundImage:nil
                                                     image:nil];
    stoneobs_temp_buyButton.titleEdgeInsets = UIEdgeInsetsMake(0, 15 + 20, 0, 0);
    [stoneobs_temp_buyButton stoneobs_fun_touch_handle:^(UIButton *sender) {
        if (handle) {
            handle();
        }
    }];
    stoneobs_temp_buyButton.tag = 20000;
    UIView *stoneobs_temp_greenView = [[UIView alloc] initWithFrame:CGRectMake(15, 0, 8, 20)];
    stoneobs_temp_greenView.backgroundColor = STONEOBS_UI_THEME_BACKGROUNDCOLOR;
    [stoneobs_temp_buyButton addSubview:stoneobs_temp_greenView];
    stoneobs_temp_greenView.centerY = stoneobs_temp_buyButton.height/2;
    STLabel * stoneobs_temp_titleLable = [[STLabel alloc] initWithFrame:CGRectMake(0, 0, 200, 44)
                                                     text:@"更多"
                                                textColor:STONEOBS_UI_FIRST_TEXT_COLOR
                                                     font:14
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentRight];
//    [buyButton addSubview:titleLable];
    
    UIImageView * stoneobs_temp_imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"happy_game_icon_getmore"]];
    stoneobs_temp_imageView.right = STONEOBS_UI_UISCREEN_WIDTH - 10;
    stoneobs_temp_imageView.centerY = 22;
    [stoneobs_temp_buyButton addSubview:stoneobs_temp_imageView];
    stoneobs_temp_imageView.hidden = !showRightGo;
    stoneobs_temp_titleLable.right = stoneobs_temp_imageView.left - 10;
    
    if (!showRightGo) {
        stoneobs_temp_imageView.hidden = YES;
        stoneobs_temp_titleLable.hidden = YES;
    }
    return stoneobs_temp_buyButton;
}
//底部按钮
+ (STButton*)stoneobs_fun_titleThemeBottomButtonWithTile:(NSString*)title  actionHandle:(void(^)(UIButton * sender))actionHandle{
    
    STButton * confimButton = [[STButton alloc] initWithFrame:CGRectMake(0, 0, STONEOBS_UI_UISCREEN_WIDTH , 49)
                                                        title:title
                                                   titleColor:STONEOBS_UI_FIRST_TEXT_COLOR
                                                    titleFont:18
                                                 cornerRadius:0
                                              backgroundColor:STONEOBS_UI_THEME_BACKGROUNDCOLOR
                                              backgroundImage:nil
                                                        image:nil];
    [confimButton stoneobs_fun_touch_handle:^(UIButton *sender) {
        if (actionHandle) {
            actionHandle(sender);
        }
    }];
    confimButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    confimButton.top = TMUtils.stoneobs_fun_get_tabBarTop;
    return confimButton;
}
+ (void)stoneobs_fun_debugSimulationNetWorkWithHadle:(void (^)(NSInteger, NSMutableArray *))handle{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSInteger num = 0.5;
    NSInteger randrom = arc4random()%40 + 10;
    NSMutableArray * array = [NSMutableArray new];
    for (NSInteger i = 0; i < randrom; i ++) {
        [array addObject:@(i)];
    }
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(num * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [SVProgressHUD dismiss];
        if (handle) {
            handle(num,array);
        }
    });
}
+ (void)stoneobs_fun_sendNoJsonRequestWithUrl:(NSString *)url
                          params:(NSDictionary *)params
                          isPost:(BOOL)isPost
                          handle:(void (^)(BOOL, NSString *))handle{
    
    AFHTTPSessionManager * manger = [AFHTTPSessionManager manager];
    manger.requestSerializer = [AFHTTPRequestSerializer serializer];
    manger.responseSerializer = [AFHTTPResponseSerializer serializer];
    manger.requestSerializer.timeoutInterval = 20;
    manger.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json", @"text/plain", @"text/html", nil];

    
    
}
+ (STButton *)stoneobs_fun_titleButtonViewWithTile:(NSString *)title showRightGo:(BOOL)showRightGo handle:(void (^)(void))handle{
    STButton * buyButton = [[STButton alloc] initWithFrame:CGRectMake(0, 0, STONEOBS_UI_UISCREEN_WIDTH, 44)
                                                     title:title
                                                titleColor:STONEOBS_UI_FIRST_TEXT_COLOR
                                                 titleFont:16
                                              cornerRadius:0
                                           backgroundColor:nil
                                           backgroundImage:nil
                                                     image:nil];
    buyButton.titleEdgeInsets = UIEdgeInsetsMake(0, 15+15, 0, 0);
    buyButton.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:14];
    [buyButton stoneobs_fun_touch_handle:^(UIButton *sender) {
        if (handle) {
            handle();
        }
    }];
    buyButton.tag = 10000;
    UIView *greenView = [[UIView alloc] initWithFrame:CGRectMake(15, 0, 8, 20)];
    greenView.backgroundColor = STONEOBS_UI_THEME_BACKGROUNDCOLOR;
    [buyButton addSubview:greenView];
    greenView.centerY = buyButton.height / 2;
    
    STLabel * titleLabel = [[STLabel alloc] initWithFrame:CGRectMake(0, 0, 200, buyButton.height)
                                                     text:@"查看更多"
                                                textColor:STONEOBS_UI_THIRD_TEXT_COLOR
                                                     font:14
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentRight];
    titleLabel.hidden = !showRightGo;
    titleLabel.right = STONEOBS_UI_UISCREEN_WIDTH - 35;
    [buyButton addSubview:titleLabel];
    
    UIImageView * stoneobs_ui_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 7, 12)];
    stoneobs_ui_imageView.image = [UIImage imageNamed:@"圆角矩形580"];
    [buyButton addSubview:stoneobs_ui_imageView];
    stoneobs_ui_imageView.centerY = buyButton.height / 2;
    stoneobs_ui_imageView.right = buyButton.width - 15;
    stoneobs_ui_imageView.hidden = !showRightGo;
    return buyButton;
}


+ (void)stoneobs_fun_makeViewToThemeGrdualColor:(UIView *)view{

    view.backgroundColor = [UIColor colorWithGradientStyle:UIGradientStyleTopToBottom withFrame:view.bounds andColors:@[WH_UICOLOR_FROM_RGBA(0x4AD941),WH_UICOLOR_FROM_RGBA(0x37CF34)]];
}


//随即一个数字，出现偶数和奇数
+ (void)stoneobs_fun_randNumberWithOuHandle:(void(^)(void))oushuHandle jiHandle:(void(^)(void))jiHandle{
    NSInteger num = arc4random()%1000;
    if (num<100) {
        if (oushuHandle) {
            oushuHandle();
        }
    }else{
        if (jiHandle) {
            jiHandle();
        }
    }
}
//上传图片
+ (void)stoneobs_fun_uploadImage:(UIImage *)image
     progressHandle:(void (^)(CGFloat))progressHandle
             handle:(void (^)(BOOL, NSString *, NSString *))handle{
    if (!image) {
        return;
    }
    NSString * url = [NSString stringWithFormat:@"%@%@",RELEASE_SERVER_URL_HEADER,@"m/upload"];
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * paramDic = [NSMutableDictionary new];
    NSString * token ;//= STTools_UserManger.defult.stoneobs_fun_get_loginedUser.token;
    if (token.length) {
        [paramDic setObject:token forKey:@"token"];
    }
    [[STTools_NetWrokManger defaultClient] stoneobs_fun_imagerequestWithPath:url parameters:paramDic image:image imageName:@"happy_game_file" progress:^(NSProgress *progress) {
        if (progressHandle) {
            progressHandle(progress.fractionCompleted);
        }
    } success:^(NSURLSessionDataTask *operation, id responseObject) {
        [SVProgressHUD dismiss];
        NSDictionary * dic = responseObject;
        NSString * url = dic[@"URL"];
        NSString * path = dic[@"PATH"];
        
        if (handle) {
            handle(YES,url,path);
        }
        
    } failure:^(NSString *stateCode, STTools_Error *error, NSError *originError) {
        [SVProgressHUD dismiss];
        if (handle) {
            handle(NO,nil,@"");
        }
    }];
}

//上传多张图片
+ (void)stoneobs_fun_uploadMoreImage:(NSArray<UIImage*>*)imageArray handle:(void (^)(BOOL, NSArray *, NSArray *))handle{
    NSMutableArray * imageurlArray = NSMutableArray.new;
    NSMutableArray * imagePathsArray = NSMutableArray.new;
    dispatch_group_t group = dispatch_group_create();
    for (UIImage * image in imageArray) {
        dispatch_group_enter(group);
        [TMUtils stoneobs_fun_uploadImage:image progressHandle:nil handle:^(BOOL success, NSString *url,NSString *path) {
            if (url.length) {
                if (url.length && path.length) {
                    [imageurlArray addObject:url];
                    [imagePathsArray addObject:path];
                }
                
            }
            dispatch_group_leave(group);
        }];
    }
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        if (handle) {
            handle(YES,imageurlArray,imagePathsArray);
        }
    });
}

+ (NSString*)stoneobs_fun_get_serverUrlHeader{

#if DEBUG
    return @"http://api.nsjgw.store/";
    
#endif
    return @"https://api.nsjgw.store/";
}
+ (NSString*)stoneobs_fun_get_rc_app_key{
    bool  isDebug = [NSUserDefaults.standardUserDefaults boolForKey:WH_HUANJ_USE];
    if (isDebug) {
        return @"4z3hlwrv4o42t";
    }
    return @"25wehl3u2s21w";
}
+ (STTools_BaseViewController*)stoneobs_fun_get_currentTopController{
    UITabBarController * tabar =  (id)UIApplication.sharedApplication.delegate.window.rootViewController;
    STTools_BaseViewController * vc;
    if ([tabar isKindOfClass:UITabBarController.class]) {
      vc = tabar.selectedViewController.childViewControllers.firstObject.rt_navigationController.rt_viewControllers.lastObject;
    }
    if ([tabar isKindOfClass:UINavigationController.class]) {
        vc = tabar.rt_navigationController.rt_viewControllers.lastObject;
    }

    return vc;
}
+ (STTools_BaseViewController*)stoneobs_fun_get_currentTopController_lastControllerIfNav{
    UITabBarController * tabar =  (id)UIApplication.sharedApplication.delegate.window.rootViewController;
    STTools_BaseViewController * vc;
    if ([tabar isKindOfClass:UITabBarController.class]) {
        NSArray * array = TMUtils.stoneobs_fun_get_currentTopController.rt_navigationController.rt_viewControllers;
        vc =array[array.count - 2];
    }
    
    return vc;
}
+ (BFShOrderHomeViewController*)shorderHomeControllerIfExit{
    UITabBarController * tabar =  (id)UIApplication.sharedApplication.delegate.window.rootViewController;
    STTools_BaseViewController * vc;
    NSArray * array;
    if ([tabar isKindOfClass:UITabBarController.class]) {
      array = tabar.selectedViewController.childViewControllers.firstObject.rt_navigationController.rt_viewControllers;
    }
    if ([tabar isKindOfClass:UINavigationController.class]) {
        array = tabar.rt_navigationController.rt_viewControllers;
    }
    for (UIViewController * vc in array) {
        if ([vc isKindOfClass:BFShOrderHomeViewController.class]) {
            return vc;
        }
    }
    return nil;
}
+ (void)goto_good_detail_witdh_id:(NSString*)good_id{
    THGoodDetailViewController * vc = THGoodDetailViewController.new;
    vc.g_id = good_id;
    [TMUtils.stoneobs_fun_get_currentTopController pushViewController:vc animated:1];
}
+ (void)goto_search_controller{
    [TMUtils.stoneobs_fun_get_currentTopController pushViewController:THSearchViewController.new animated:1];
}
+ (void)goto_user_info_witdh_id{
    
}
//去帖子详情
+ (void)goto_tiezi_detail:(THTieZiModel*)model dataSouce:(NSArray*)dataSouce{

    if (model.type.intValue == 2) {
        THVideoDetailViewController * vc = THVideoDetailViewController.new;
        vc.model = model;
        vc.dataSouce = dataSouce;
        [TMUtils.stoneobs_fun_get_currentTopController pushViewController:vc animated:1];
    }else{
        THTieZiDetailViewController * vc = THTieZiDetailViewController.new;
        vc.model = model;
        [TMUtils.stoneobs_fun_get_currentTopController pushViewController:vc animated:1];
        
    }
}
+ (void)goto_share_witdh_url:(NSString*)url{
    THShareViewController * vc = THShareViewController.new;
    
    vc.tie_id = url;

    [TMUtils.stoneobs_fun_get_currentTopController presentViewController:vc animated:NO completion:nil];
}
+ (void)goto_user_info_witdh_id:(NSString *)user_id{
    if ([user_id isEqualToString:STTools_UserManger.defult.stoneobs_fun_get_loginedUser.user_id]) {
        [TMUtils.stoneobs_fun_get_currentTopController pushViewController:THMyUserInfoViewController.new animated:1];
    }else{
        THOtherUserInfoViewController * vc = THOtherUserInfoViewController.new;
        vc.user_id = user_id;
        [TMUtils.stoneobs_fun_get_currentTopController pushViewController:vc animated:1];
    }
    
}
//自动处理banner
+ (void)auto_deal_banner:(THBannerModel*)model{
    //1外联  2 商品详情 3咨询详情
    if (model.type.intValue == 1) {
        STTools_WebViewController * vc =  [STTools_WebViewController.alloc initWithUrl:model.url];
        vc.forceTitle = model.title;
        [TMUtils.stoneobs_fun_get_currentTopController pushViewController:vc animated:1];
    }

    if (model.type.intValue == 2) {
        THGoodDetailViewController * vc =  [THGoodDetailViewController.alloc init];
        vc.g_id = model.url;
        [TMUtils.stoneobs_fun_get_currentTopController pushViewController:vc animated:1];
    }
    
    if (model.type.intValue == 3) {
        [THTreeHoleDataController getTieZiDetailWithPosts_id:model.url handle:^(THTieZiModel * _Nonnull model) {
            
            if (model.type.intValue == 1) {
                THTieZiDetailViewController * vc =  THTieZiDetailViewController.new;
                vc.model = model;
                [TMUtils.stoneobs_fun_get_currentTopController pushViewController:vc animated:1];
            }else{
                THVideoDetailViewController * vc =  THVideoDetailViewController.new;
                vc.model = model;
                [TMUtils.stoneobs_fun_get_currentTopController pushViewController:vc animated:1];
            }
            
        }];

        

    }
}
@end

